
layui.use(['form','layer','layedit','laydate','upload','table'],function(){
    $ = layui.jquery;
    var layer = layui.layer ,table = layui.table,laydate = layui.laydate,form = layui.form;
    //第一个实例
    var indexx = table.render({
        elem: '#demo'
        ,id:'testReload'
        ,height: "auto"
        ,method:"post"
        ,url: baseUrl+'perspective/openPerspective' //数据接口
        ,request: {
            pageName: 'pageNum' //页码的参数名称，默认：page
            ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        }
        ,page: false //开启分页
        ,cols: [[ //表头
            {field:"userid", title: '用户id',align:'center'}
            // ,{field:"roomCard", title: '俱乐部房卡',align:'center',event:'edit'}
            ,{field:"userName", title: '用户名称',align:'center'}
            ,{field: "gameN", title: '牛牛',align:'center',templet:function (d){
                    if(d.gameN == 1){
                        return "<span style='color: #0069ff'>"+"透视已开启" +"</span>";
                    }
                    if(d.gameN == 0){
                        return "<span style='color: #0069ff'>"+"透视未开启" +"</span>";
                    }
                }}
            ,{field: "gameZ", title: '牛牛',align:'center',templet:function (d){
                    if(d.gameZ == 1){
                        return "<span style='color: #0069ff'>"+"透视已开启" +"</span>";
                    }
                    if(d.gameZ == 0){
                        return "<span style='color: #0069ff'>"+"透视未开启" +"</span>";
                    }
                }}
            ,{field: 'createTime', title: '创建时间',align:'center',sort:true, width:160, toolbar: '#createTime'}
            ,{fixed: 'right',   title: '操作', width:150, align:'center',event:"show", toolbar: '#barDemo'}
            ,{fixed: 'right',   title: '操作', width:150, align:'center',event:"show1", toolbar: '#barDemo1'}
            // ,{fixed: 'right',   title: '操作', width:150, align:'center', toolbar: '#barDemo1'}
        ]]

    });

    //监听锁定操作
    form.on('switch(switchTest)',function(formObj) {
        table.on('tool(test)',function(obj) {
            if (obj.event === 'show') {
                if (formObj.elem.checked == true) {
                    var lock = 1;
                } else {
                    var lock = 0;
                }
            }
            $.ajax({
                type: 'post',
                url: baseUrl+"perspective/editPerspective",
                data: {'userid': obj.data.userid , "gameN" : lock },
                async:false,
                dataType: 'json',
                success: function(res){
                    if(res.meta.code==200){
                        //加载层
                        var index = layer.load(0, {shade: false,time:500} ); //0代表加载的风格，支持0-2
                        setTimeout(function(){ layer.msg(''+res.meta.msg+'',{icon:1,time:1000});index.closed}, 500);
                        if(lock == 0){
                            layer.tips('已取消透视', formObj.othis,{
                                tips: 1
                            });
                            obj.update({
                                gameN: "<span style='color: green'>未开始透视</span>"
                            });
                            // window.location.reload();
                        }else{
                            layer.tips('开启透视', formObj.othis,{
                                tips: 3
                            });
                            obj.update({
                                gameN: "<span style='color: red'>已开启透视</span>"
                            });

                        }
                    }else{
                        //加载层
                        var index = layer.load(0, {shade: false,time:1000} ); //0代表加载的风格，支持0-2
                        setTimeout(function(){ layer.msg(''+res.meta.msg+'',{icon:2,time:1000});index.closed}, 1000);
                    }
                }
            });
        });
    });

    //监听锁定操作
    form.on('switch(switchTest1)',function(formObj) {
        table.on('tool(test)',function(obj) {
            if (obj.event === 'show1') {
                if (formObj.elem.checked == true) {
                    var locks = 1;
                } else {
                    var locks = 0;
                }

            }
            $.ajax({
                type: 'post',
                url: baseUrl+"perspective/editPerspective",
                data: {'userid': obj.data.userid , "gameZ" : locks },
                async:false,
                dataType: 'json',
                success: function(res){
                    if(res.meta.code==200){
                        //加载层
                        var index = layer.load(0, {shade: false,time:500} ); //0代表加载的风格，支持0-2
                        setTimeout(function(){ layer.msg(''+res.meta.msg+'',{icon:1,time:1000});index.closed}, 500);

                        if(locks==0){

                            layer.tips('已取消透视', formObj.othis,{
                                tips: 1
                            });
                            obj.update({
                                gameZ: "<span style='color: green'>未开始透视</span>"
                            });
                            // window.location.reload();
                        }else{
                            layer.tips('开启透视', formObj.othis,{
                                tips: 3
                            });
                            obj.update({
                                gameZ: "<span style='color: red'>已开启透视</span>"
                            });

                        }
                    }else{
                        //加载层
                        var index = layer.load(0, {shade: false,time:1000} ); //0代表加载的风格，支持0-2
                        setTimeout(function(){ layer.msg(''+res.meta.msg+'',{icon:2,time:1000});index.closed}, 1000);
                    }
                }
            });
        });
    });

    var $ = layui.$, active = {
        reload: function(){
            var demoReload = $('#demoReload');

            //执行重载
            table.reload('testReload', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    userid: demoReload.val()
                }
            });
        }
    };

    $('.demoTable .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

})

